package com.zyg.controller;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zyg.pojo.ClassEx; // 修改了对应的实体类
import com.zyg.service.ClassExService; // 修改了对应的服务类
import com.zyg.util.LayData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/classex") // 修改了请求路径
public class ClassExController {

    @Autowired
    private ClassExService classExService; // 修改了注入的服务类

    @RequestMapping("/tolist")
    public String tolist() {
        return "classex/list"; // 修改了视图路径
    }

    @RequestMapping("/list")
    @ResponseBody
    public String list(ClassEx classObj, @RequestParam(value = "page", defaultValue = "1") Integer page, @RequestParam(value = "limit", defaultValue = "10") Integer limit) {
        // 查询数据
        LayData layData = new LayData();
        QueryWrapper<ClassEx> qw = new QueryWrapper<>();
        qw.like(classObj.getClassName() != null && !"".equals(classObj.getClassName()), "class_name", classObj.getClassName()); // 修改了属性名
        long totalCount = classExService.count(qw);
        layData.setCount((int) totalCount);
        Page<ClassEx> page1 = new Page<>(page, limit);
        layData.setData(classExService.list(page1, qw));
        layData.setCode(0);
        layData.setMsg("查询成功");
        // 数据返回
        String data = JSON.toJSONString(layData);
        return data;
    }

    @RequestMapping("/toadd")
    public String toAdd() {
        return "classex/add"; // 修改了视图路径
    }

    @RequestMapping("/addSave")
    @ResponseBody
    public String addSave(ClassEx classObj) {
        boolean flag = classExService.save(classObj);
        if (flag) {
            return JSON.toJSONString(new LayData(0, "添加成功"));
        } else {
            return JSON.toJSONString(new LayData(500, "添加失败"));
        }
    }

    @RequestMapping("/toupdate/{id}")
    public String toUpdate(@PathVariable("id") int id, Model model) {
        ClassEx classObj = classExService.getById(id); // 修改了实体类
        model.addAttribute("classObj", classObj); // 修改了变量名
        return "classex/update"; // 修改了视图路径
    }

    @RequestMapping("/toinfo/{id}")
    public String toInfo(@PathVariable("id") int id, Model model) {
        ClassEx classObj = classExService.getById(id); // 修改了实体类
        model.addAttribute("classObj", classObj); // 修改了变量名
        return "classex/info"; // 修改了视图路径
    }

    @RequestMapping("/updateSave")
    @ResponseBody
    public String updateSave(ClassEx classObj) {
        boolean flag = classExService.updateById(classObj);
        if (flag) {
            return JSON.toJSONString(new LayData(0, "更新成功"));
        } else {
            return JSON.toJSONString(new LayData(500, "更新失败"));
        }
    }

    @RequestMapping("/delete/{id}")
    @ResponseBody
    public String deleteClass(@PathVariable("id") int id) {
        boolean flag = classExService.removeById(id);
        if (flag) {
            return JSON.toJSONString(new LayData(0, "删除成功"));
        } else {
            return JSON.toJSONString(new LayData(500, "删除失败"));
        }
    }
}
